package com.Trainer.TestInterface;

import com.Trainer.EvaluateInterface.EvaluateInterface;
import com.data.TrainerData;
import com.data.UpData;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class TestInterface extends JPanel {
    //属性
    TestBottomLeftPanel testBottomLeftPanel;
    TestUpPanel testUpPanel;
    TestBottomRightPanel testBottomRightPanel;
    TrainerData trainerData;
    UpData upData;
    List<TrainerData> problems = new ArrayList<>();
    int id=0;
    int temId = -1;
    String patten ="";
    EvaluateInterface evaluateInterface ;
    int totalTime=0;
    ActionListener taskPerform = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            totalTime++;
           if(!testBottomLeftPanel.setTime()){
               submitListener();

           }
        }
    };
    Timer time = new Timer(1000,taskPerform);

    //构造器
    public TestInterface(int width,int height){
        trainerData=new TrainerData();
        upData=new UpData();
        testUpPanel= new TestUpPanel(0,0,900,100,upData);
        testBottomRightPanel=new TestBottomRightPanel(650,100,250,400);
        testBottomLeftPanel = new TestBottomLeftPanel(0, 100, 650, 400,trainerData);
        evaluateInterface = new EvaluateInterface(width,height);

        //布局
        setBounds(0,0,width,height);
        setLayout(null);
        add(testUpPanel);
        add(testBottomLeftPanel);
        add(testBottomRightPanel);

        add(evaluateInterface);
        evaluateInterface.setVisible(false);

        //事件

        testBottomRightPanel.getPlusBtn().addActionListener((e)->{
            if (id==0){
                testUpPanel.getCurrentPattern().setText("加法模式");
                patten = "加法";
                evaluateInterface.getPattern().setText("加法模式");
            }
        });

        testBottomRightPanel.getSubBtn().addActionListener((e)->{
            if (id==0){
                testUpPanel.getCurrentPattern().setText("减法模式");
                patten = "减法";
                evaluateInterface.getPattern().setText("减法模式");
            }
        });

        testBottomRightPanel.getPlusSubBtn().addActionListener((e)->{
            if (id==0){
                testUpPanel.getCurrentPattern().setText("加减混合模式");
                patten = "加减法";
                evaluateInterface.getPattern().setText("加减混合模式");
            }

        });

        testBottomLeftPanel.getStart().addActionListener((e)->{
            if(!patten.equals("")){
                testBottomLeftPanel.getStart().setVisible(false);
                testBottomLeftPanel.getStart().setEnabled(false);
                testBottomLeftPanel.getConfirmBtn().setVisible(true);
                testBottomLeftPanel.getNextBtn().setVisible(true);
                testBottomLeftPanel.getConfirmBtn().setEnabled(true);
                testBottomLeftPanel.getNextBtn().setEnabled(true);

                temId++;
                id++;
                TrainerData data=new TrainerData();
                data.setTrainerData(patten);
                problems.add(data);
                setValue(data);
                //计时功能
                time.start();
            }
        });

        //上一题
        testBottomLeftPanel.getConfirmBtn().addActionListener((e)->{
            problems.get(temId).setAns(testBottomLeftPanel.getAnsText().getText());
            if (temId==19){
                testBottomLeftPanel.getNextBtn().setVisible(true);
                testBottomLeftPanel.getNextBtn().setEnabled(true);
                testBottomLeftPanel.getSubmit().setVisible(false);
                testBottomLeftPanel.getSubmit().setEnabled(false);
            }
            if (temId>0){
                temId--;
                setValue(problems.get(temId));
            }
        });
        //下一题
        testBottomLeftPanel.getNextBtn().addActionListener((e)->{

            if(!patten.equals("")){
                if (temId>-1){
                    problems.get(temId).setAns(testBottomLeftPanel.getAnsText().getText());
                }
                temId++;
            }
            if(temId+1<=id){
                setValue(problems.get(temId));
            }else {
                id++;
                TrainerData data=new TrainerData();
                data.setTrainerData(patten);
                problems.add(data);
                setValue(data);
            }

            if (temId==19){
                testBottomLeftPanel.getNextBtn().setVisible(false);
                testBottomLeftPanel.getNextBtn().setEnabled(false);
                testBottomLeftPanel.getSubmit().setVisible(true);
                testBottomLeftPanel.getSubmit().setEnabled(true);
            }
        });

        //提交
        testBottomLeftPanel.getSubmit().addActionListener((e)->{
            submitListener();
        });

        //evalua界面的返回 按钮

    }

    //方法

    public JButton getBackBtn(){
        return testBottomRightPanel.getBackBtn();
    }
    public JButton getEvaluateBackBtn(){
        return evaluateInterface.getBack();
    }
    public void setValue(TrainerData trainerData){
        testBottomLeftPanel.getOperand1().setText(""+ trainerData.getOp1());
        testBottomLeftPanel.getOperand2().setText(""+ trainerData.getOp2());
        testBottomLeftPanel.getOperator1().setText(""+ trainerData.getOpLabel());
        testBottomLeftPanel.getAnsText().setText(""+ trainerData.getAns());
        testUpPanel.getCorrect().setText(""+(temId+1));
    }

    public void setProblems(List<TrainerData> problems) {
        this.problems = problems;
    }

    /**
     * 对布局中数据进行初始化
     */
    public void reset(String time){
        trainerData=new TrainerData();
        upData=new UpData();
        problems = new ArrayList<>();
        id=0;
        temId = -1;
        patten ="";
        testUpPanel.getCurrentPattern().setText("请选择你的模式");
        testBottomLeftPanel.getTime().setText(time);
        totalTime = 0;
        setValue(trainerData);
        evaluateInterface.reset(time);
    }

    public void resetLayout(){
        testBottomLeftPanel.getSubmit().setVisible(false);
        testBottomLeftPanel.getNextBtn().setVisible(false);
        testBottomLeftPanel.getConfirmBtn().setVisible(false);
        testBottomLeftPanel.getSubmit().setEnabled(false);
        testBottomLeftPanel.getNextBtn().setEnabled(false);
        testBottomLeftPanel.getConfirmBtn().setEnabled(false);
        testBottomLeftPanel.getStart().setVisible(true);
        testBottomLeftPanel.getStart().setEnabled(true);
        testUpPanel.setVisible(true);
        testBottomLeftPanel.setVisible(true);
        testBottomRightPanel.setVisible(true);
        evaluateInterface.setVisible(false);

    }

    /**
     * 获得20道题目的正确数量
     * @return 返回20道题目中正确题目的数量
     */
    public int getAnsCorrectNum(){
        int count = 0;
        for (TrainerData problem : problems) {
            if (problem.judgeCorrect())
                count++;
        }
        return count;
    }

    public int getDoneNum(){
        int count = 0;
        for (TrainerData problem : problems) {
            if (!problem.getAns().equals(""))
                count++;
        }
        return count;
    }

    public Timer getTime() {
        return time;
    }

    public String getEvaluate(int correct){
        if(correct>=19){
            return "S+";
        }else if(correct>=18){
            return "S";
        }else if(correct>=16){
            return "A";
        }else if(correct>=14){
            return "B";
        }else if(correct>=12){
            return "C";
        }else{
            return "D";
        }
    }

    public void submitListener(){
        problems.get(temId).setAns(testBottomLeftPanel.getAnsText().getText());
        float correctRate = (float) ((float)getAnsCorrectNum()/20.0)*100;
        int correct = getAnsCorrectNum();
        evaluateInterface.setTotal(""+20);
        evaluateInterface.setDone(""+getDoneNum());
        evaluateInterface.setCorrect(""+correct);
        evaluateInterface.setTime(""+totalTime);
        evaluateInterface.setCorrectRate(""+correctRate);
        evaluateInterface.setGrade(getEvaluate(correct));

        testUpPanel.setVisible(false);
        testBottomLeftPanel.setVisible(false);
        testBottomRightPanel.setVisible(false);
        evaluateInterface.setVisible(true);
        time.stop();
    }
}
